import Router from '@/router/router'
import Config from '@/enum/setting'
import {getToken} from "@/utils/token.js";
import store from '@/store'
import {resolveRouter} from "@/store/modules/permission.js";
import {getLoginMenus} from "@/api/sysMenu";

// 页面白名单
const whiteList = ['/login']
/**
 * 路由前置守卫
 * 当路由发生时会触发调用
 */
Router.beforeEach(async (to, from, next) => {
    // 页面标题
    if (to.meta.title) {
        document.title = to.meta.title + ' - ' + Config.title
    }
    // 判断是否携带token
    if (getToken()) {
        // 已登录且要跳转的页面是登录页
        if (to.path === '/login') {
            next({path: '/testPage'})
        } else {
            // 判断是否拉取用户信息
            if (store.getters.roles.length === 0) {
                // 装载客户信息
                store.dispatch('getLoginUserInfo').then(() => {
                    loadMenus()
                }).catch(() => {
                    // 为了重新实例化vue-router对象 避免bug
                    location.reload()
                })
            } else {
                if (store.getters.menuFlag) {
                    store.dispatch('loadMenus').then(r => {
                    })
                    loadMenus()
                }
            }
        }
        next()
    } else {
        // 判断是否属于免登录白名单
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
        }
    }
})


/**
 * 调用接口装载路由
 */
export const loadMenus = () => {
    // 调用接口获取路由
    getLoginMenus().then(res => {
        if (res.state) {
            let menus = res.data
            // 路由动态加载
            let routerList = resolveRouter(menus)
            routerList.push({path: '/:catchAll(.*)', redirect: '/404', hidden: true})
            let routes = resolveRoutes(routerList);
            // store.dispatch('GenerateRoutes', routes).then(() => {
            //     // 将处理好的路由放在router里面
            //     for (let routerListElement of routes) {
            //         Router.addRoute(routerListElement)
            //     }
            // })
            // store.dispatch('GenerateRoutes', routerList).then(() => {
            //     // 将处理好的路由放在router里面
            //     for (let routerListElement of routerList) {
            //         Router.addRoute(routerListElement)
            //     }
            // })
        } else {
            // let routerList = []
            // routerList.push({path: '/:catchAll(.*)', redirect: '/404', hidden: true})
            // store.dispatch('GenerateRoutes', routerList).then(() => { // 存储路由
            //     Router.addRoute(routerList) // 动态添加可访问路由表
            // })
            let routerList = []
            // routerList.push({path: '/:catchAll(.*)', redirect: '/404', hidden: true})
            // store.dispatch('GenerateRoutes', routerList).then(() => { // 存储路由
            //     Router.addRoute(routerList) // 动态添加可访问路由表
            // })
        }
    })



    function resolveRoutes(routerList) {
        let resolveMenus = []
        routerList.forEach(router => {
            if (router.path!=='/' && router.path!=='/login'){
                if (router.meta && router.meta.affix) {
                    const tagPath = router.path
                    resolveMenus.push({
                        fullPath: tagPath,
                        path: tagPath,
                        name: router.name,
                        meta: {...router.meta},
                        children:[]
                    })
                }
                if (router.children) {
                    const tempTags = resolveRoutes(router.children, router.path)
                    if (tempTags.length >= 1) {
                        resolveMenus.forEach(rs=>{
                            if (rs.path===router.path){
                                rs.children = [...rs.children, ...tempTags]
                            }
                        })
                    }
                }
            }
        })
        // 过滤一下 ‘/’ '/login'
        return resolveMenus;
    }
}